const axios = require("axios")
const qs = require("qs")
const cache = require("./config/data.json")

const {
    login_token,
    domain
} = cache

const http = axios.create({
    baseURL:"https://dnsapi.cn/",
    timeout:20000,
    transformRequest:[function(data){
        data = qs.stringify(data)
        data = decodeURIComponent(data)
        return data;
    }]
})

const baseParams = {
    login_token,
    format:"json",
    lang:"cn",
    record_type:"A",
    record_line:"默认",
    domain,
}

http.interceptors.request.use(function(config){
    const {method} = config
    if(method==="get"){
        if(!config.params){
            config.params = {}
        }
        Object.assign(config.params,baseParams)
    }else{
        if(!config.data){
            config.data = {}
        }
        Object.assign(config.data,baseParams)
    }
    return config;
})

http.interceptors.response.use(async function(response){

    // console.log(data)
    if(response.data.status && response.data.status.code!=="1"){
        return Promise.reject(response)
    }
    return response;
})

module.exports = http